﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.BLL.SAServiceRef;
using SalePartial.BLL;
using Telerik.Web.UI;
using System.Drawing;

public partial class SAClientWeb_FamilyDetailFrm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Guid familyId;
            //if (HttpContext.Current.Session["familyId"] != null)
            if (Request["familyId"]!=null)
            {
                HttpContext.Current.Session["familyId"] = Request["familyId"];
                //familyId = new Guid(HttpContext.Current.Session["familyId"].ToString());
                familyId = new Guid(Request["familyId"].ToString());

                FamilyDetailFrm_Load(familyId);
            }
            else
            {
                SABLL.ShowInfoMsg(this,@"无法查看，请在零件列表中至少选中一个零件！");
                return;
            }
            
        }
    }

    private void FamilyDetailFrm_Load(Guid familyId)
        {
            //get data.
            FamilyInfo m_familyInfo = SABLL.GetFamilyInfo(familyId);

            FamilyAttrInfo familyAttr = SABLL.GetFamilyAttrInfo(m_familyInfo.ID);
            
            if (familyAttr == null)
            {
                SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
                return;
            }

            //string templateFilePath, modelPicPath, drawPicPath;
            //templateFilePath = System.IO.Path.Combine(GetTempFolder(familyId), familyAttr.TemplateFileName);
            //modelPicPath = System.IO.Path.Combine(GetTempFolder(familyId), familyAttr.ModelPicName);
            //drawPicPath = System.IO.Path.Combine(GetTempFolder(familyId), familyAttr.DrawPicName);

            //templateFilePath = SABLL.SaveBinaryByPath(templateFilePath, familyAttr.TemplateFileBinary);
            //modelPicPath = SABLL.SaveBinaryByPath(modelPicPath, familyAttr.ModelPicBinary);
            //drawPicPath = SABLL.SaveBinaryByPath(drawPicPath, familyAttr.DrawPicBinary);
            //this.pictureBox_modelPic.ImageUrl = modelPicPath;
            //this.pictureBox_drawPic.ImageUrl = drawPicPath;

            this.pictureBox_modelPic.DataValue = familyAttr.ModelPicBinary;
            this.pictureBox_drawPic.DataValue = familyAttr.DrawPicBinary;

            
            this.textBox_modelName.Text = m_familyInfo.Name;
            this.textBox_category.Text = SABLL.GetCategoryInfoByCateId(m_familyInfo.CategoryID).Name;

            VendorInfo vendor = SABLL.GetVendor(m_familyInfo.VendorID);
            if (vendor != null) this.textBox_vendor.Text = vendor.Name;

            StandardInfo standard = SABLL.GetStandard(m_familyInfo.StandardID);
            if (standard != null) this.textBox_standard.Text = standard.Name;

            this.richTextBox_description.Text = familyAttr.Description;

            List<ParameterInfo> keyParameters = SABLL.GetFamilyKeyParameters(m_familyInfo.ID);
            if (keyParameters == null)
            {
                SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
                return;
            }
            else
            {
                this.dataGridView_driverParamsList.DataSource = keyParameters;
                this.dataGridView_driverParamsList.DataBind();
            }
        }

    protected void dataGridView_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item is GridDataItem)
        {

            GridDataItem itm = (GridDataItem)e.Item;

            //bool IsDriver = (bool)itm.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IsDriver"];
            bool IsDriver = true;

            TableCell cellCust = (TableCell)itm["IsCustom"];
            TableCell cellDri = (TableCell)itm["IsDriver"];

            bool.TryParse(cellDri.Text, out IsDriver);

            if (!IsDriver)
            {
              //  cell.Text = false;
                cellCust.Enabled = true;
                cellCust.BackColor = Color.LightGray;

                cellDri.Enabled = true;
                cellDri.BackColor = cellCust.BackColor;
            }
            else
            {
                TableCell cellName = (TableCell)itm["Name"];
                cellName.Font.Bold = true;
                cellName.ForeColor = Color.Red;
                //cellCust.Value = curKeyParam.IsCustom;
                cellCust.Enabled = false;
                cellDri.Enabled = false;
            }

            

        }

    }
    
}